package guoanmaker.provider.business.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;

import guoanmaker.provider.business.modelnobind.DataStatistics;

@Service
public class DataStatisticsService implements DataStatisticsServiceInterface {

	@PersistenceContext
	private EntityManager manager;

	@Override
	public Map<String, Object> routineData(String businessId) {
		Map<String,Object> map=new HashMap<String,Object>();
		DataStatistics dataStatistics=null;
		//近30天下单金额
		Double nearlyThirtyDaysOrderAmount=0.0;
		//近30天下单量
		Long nearlyThirtyDaysOrderQuantity=0L;
		//近30天下单会员数
		Long nearlyThirtyDaysOrderMembership=0L;
		Calendar nows=Calendar.getInstance();
		nows.setTime(new Date());
		nows.add(Calendar.DATE, -30);
		Date date=nows.getTime();
		String jpql1="select new guoanmaker.provider.business.modelnobind.DataStatistics(sum(p.realPrice),count(1),count(DISTINCT u.id) ) "
				+ "from ProvideOrderForm p left join p.user u left join p.commodity c left join c.providerBusiness b where "
				+ "p.placeOrderTime>:date and p.orderStatus in (6,8,12,13) and b.id=:id";
		Query query1=manager.createQuery(jpql1).setParameter("date", date).setParameter("id", businessId);
		List<DataStatistics> list1=query1.getResultList();
		if(list1!=null&&list1.size()>0){
			dataStatistics=list1.get(0); 
			nearlyThirtyDaysOrderAmount=dataStatistics.getAmont();
			nearlyThirtyDaysOrderQuantity=dataStatistics.getCount();
			nearlyThirtyDaysOrderMembership=dataStatistics.getUsercount();
		}
		map.put("nearlyThirtyDaysOrderAmount", nearlyThirtyDaysOrderAmount);
		map.put("nearlyThirtyDaysOrderQuantity", nearlyThirtyDaysOrderQuantity);
		map.put("nearlyThirtyDaysOrderMembership", nearlyThirtyDaysOrderMembership);
		//平均客单价
		Double averageUnitPrice=0.0;
		try {
			averageUnitPrice=nearlyThirtyDaysOrderAmount/nearlyThirtyDaysOrderQuantity;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			averageUnitPrice=0.0;
		}
		map.put("averageUnitPrice", averageUnitPrice);
		//商品收藏量
		Long commodityCollectionQuantity=0L;
		String jpql2="select new guoanmaker.provider.business.modelnobind.DataStatistics(count(1)) "
				+ "from PersonalCollection p left join p.provideCommodity c where c.providerBusiness.id=:id";
		Query query2=manager.createQuery(jpql2).setParameter("id", businessId);
		List<DataStatistics> list2=query2.getResultList();
		if(list2!=null&&list2.size()>0){
			dataStatistics=list2.get(0); 
			commodityCollectionQuantity=dataStatistics.getCount();
		}
		map.put("commodityCollectionQuantity", commodityCollectionQuantity);
		
		//商品总数
		Long CollectionQuantity=0L;
		String jpql3="select new guoanmaker.provider.business.modelnobind.DataStatistics(count(1)) "
				+ "from ProvideCommodity c where c.providerBusiness.id=:id";
		Query query3=manager.createQuery(jpql3).setParameter("id", businessId);
		List<DataStatistics> list3=query3.getResultList();
		if(list3!=null&&list3.size()>0){
			dataStatistics=list3.get(0); 
			CollectionQuantity=dataStatistics.getCount();
		}
		map.put("CollectionQuantity", CollectionQuantity);
		return map;
	}

	@Override
	public List<DataStatistics> salesTrend(String businessId, Integer time) {
		Calendar nows=Calendar.getInstance();
		nows.setTime(new Date());
		nows.add(Calendar.DATE, -time);
		Date date=nows.getTime();
		String jpql="select new guoanmaker.provider.business.modelnobind.DataStatistics(date_format(p.placeOrderTime, '%Y-%m-%d'),count(1)) "
				+ "from ProvideOrderForm p left join p.commodity c where p.orderStatus in (6,8,12,13) and c.providerBusiness.id=:id "
				+ "and p.placeOrderTime>:date GROUP BY date_format(p.placeOrderTime, '%Y-%m-%d')";
		Query query=manager.createQuery(jpql).setParameter("date", date).setParameter("id", businessId);
		List<DataStatistics> list=query.getResultList();
		return list;
	}

	@Override
	public List<Object> salesTrendInThePastYear(String businessId) {
		Calendar nows=Calendar.getInstance();
		nows.setTime(new Date());
		nows.add(Calendar.YEAR, -1);
		Date date=nows.getTime();
		String jpql="select new guoanmaker.provider.business.modelnobind.DataStatistics(date_format(p.placeOrderTime, '%Y-%m'),count(1)) "
				+ "from ProvideOrderForm p left join p.commodity c where p.orderStatus in (6,8,12,13) and c.providerBusiness.id=:id "
				+ "and p.placeOrderTime>:date GROUP BY date_format(p.placeOrderTime, '%Y-%m')";
		Query query=manager.createQuery(jpql).setParameter("date", date).setParameter("id", businessId);
		List<DataStatistics> list=query.getResultList();
		DataStatistics dataStatistics=null;
		List<Object> listint=new ArrayList<Object>();
		int n=0;
		for (int i = 1; i < 13; i++) {
			try {
				dataStatistics=list.get(n);
				String dates=dataStatistics.getDate();
				int a=Integer.parseInt(dates.substring(5,7));
				if(a==i){
					listint.add(dataStatistics.getCount());
					n++;
				}
				else{
					listint.add(0);
				}
			} catch (Exception e) {
				listint.add(0);
			}
		}
		return listint;
	}
	
	
}
